package com.gwt.material.client.user.controller;

import com.gwt.auth.feign.AuthFeign;
import com.gwt.auth.params.TokenForm;
import com.gwt.auth.vo.AuthTokenVO;
import com.gwt.boot.constant.CommonEnum;
import com.gwt.cloud.result.ApiResult;
import com.gwt.material.client.user.api.WeChatLoginApi;
import com.gwt.material.client.user.dto.WechatAuthDTO;
import com.gwt.material.client.user.dto.WechatUserDTO;
import com.gwt.material.client.user.po.User;
import com.gwt.material.client.user.po.WechatUser;
import com.gwt.material.client.user.service.UserService;
import com.gwt.material.client.user.service.UserSourceRelService;
import com.gwt.material.client.user.service.WechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class WeChatLoginController implements WeChatLoginApi {

    @Resource
    private WechatUserService wechatUserService;

    @Resource
    private UserSourceRelService userSourceRelService;

    @Resource
    private UserService userService;

    @Autowired
    private AuthFeign authFeign;

    @Override
    public ApiResult<String> wechatQrCode() {
        return ApiResult.ok(wechatUserService.wechatQrCode());
    }

    @Override
    @Transactional
    public ApiResult<String> weChatLogin(@RequestParam String code, @RequestParam String state, HttpServletRequest request) {
        //调用微信接口
        //WechatAuthDTO wechatAuthDTO = wechatUserService.getWechatToken(code, state);
        WechatAuthDTO wechatAuthDTO = new WechatAuthDTO();
        wechatAuthDTO.setOpenid("56567567567");
        //查询是否已注册(微信用户)
        WechatUserDTO wechatUserDTO = wechatUserService.getWechatUserByOpenId(wechatAuthDTO.getOpenid());
        if (wechatUserDTO == null){
            //创建微信用户
            WechatUser wechatUser = wechatUserService.createWechatUser(wechatAuthDTO);
            //后期可根据手机号进行匹配关联
            User user = new User();
            userService.createUser(user);
            wechatUserDTO = new WechatUserDTO();
            wechatUserDTO.setUserId(user.getId());
            wechatUserDTO.setId(wechatUser.getId());
        }else {
            //匹配User表中的用户信息，并进行关联
            if (wechatUserDTO.getUserId() == null){
                User user = new User();
                userService.createUser(user);
                wechatUserDTO.setUserId(user.getId());
            }
        }
        //创建、更新用户关联关系
        userSourceRelService.refreshUserSourceRel(wechatUserDTO.getUserId(), wechatUserDTO.getId(), CommonEnum.SOURCE_WECHAT.getCode(), wechatAuthDTO.getAccess_token());
        //token创建
        TokenForm tokenForm = new TokenForm();
        tokenForm.setUserId(wechatUserDTO.getUserId());
        tokenForm.setOtherToken(wechatUserDTO.getAccessToken());
        tokenForm.setRefreshToken(wechatUserDTO.getRefreshToken());
        ApiResult<AuthTokenVO> result = authFeign.tokenCreate(tokenForm);
        if (result.getSuccess()){
            AuthTokenVO authTokenVO = result.getData();
            return ApiResult.ok(authTokenVO.getToken());
        }
        return ApiResult.error("用户授权失败！");
    }
}
